package com.tj.jinzao.presenter;

import android.util.Log;

import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BasePresenter;
import com.tj.jinzao.callback.IFamilyView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.FamilyMember;
import com.tj.jinzao.model.ResultResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 家庭成员
 * Created by wjy 2018/12/10.
 */
public class FamilyPresenter extends BasePresenter<IFamilyView<List<FamilyMember>>> {

    public FamilyPresenter(IFamilyView<List<FamilyMember>> view) {
        super(view);
    }

    public void getFamilyList() {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.getFamilyMemberList(uId), new SubscriberCallBack<List<FamilyMember>>(mView.getContext()) {
            @Override
            protected void onSuccess(List<FamilyMember> list) {
                if (mView != null) mView.onRequestSuccess(list);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }
        });
    }

    public void addFamily(Map<String, Object> map) {
        long uId = UserManager.getInstance().getUser().getId();
        map.put("uid", uId);
        Log.w("数据更新添加","qqq"+map.toString());
        addSubscription(mApiService.addFamilyMember(map), new SubscriberCallBack<FamilyMember>(mView.getContext()) {
            @Override
            protected void onSuccess(FamilyMember familyMember) {
                Log.w("数据更新添加","fff"+familyMember.toString());
                List<FamilyMember> list = new ArrayList();
                familyMember.setType(2);
                list.add(familyMember);
                if (mView != null) mView.onRequestSuccess(list);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                Log.w("数据更新添加","wwww"+response.toString());
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }
        });
    }

    public void updateFamily(long id ,Map<String, Object> map) {
        addSubscription(mApiService.updateFamilyMember(id, map), new SubscriberCallBack<FamilyMember>(mView.getContext()) {
            @Override
            protected void onSuccess(FamilyMember familyMember) {
                List<FamilyMember> list = new ArrayList();
                familyMember.setType(3);
                list.add(familyMember);
                if (mView != null) mView.onRequestSuccess(list);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }
        });
    }

    public void getFamilyMember(long id) {
        addSubscription(mApiService.getFamilyMember(id), new SubscriberCallBack<FamilyMember>(mView.getContext()) {
            @Override
            protected void onSuccess(FamilyMember familyMember) {
                List<FamilyMember> list = new ArrayList();
                list.add(familyMember);
                if (mView != null) mView.onRequestSuccess(list);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }
        });
    }

    @Override
    protected void onUploadSuccess(String localPath, String response) {
        if (mView != null) mView.onUploadSuccess(localPath, response);
    }
}